clear all
log using "log_file_stata_application", text replace
********************************************************
******          Replication Code for              ******
******     Bayesian versus maximum likelihood     ******
******      estimation of treatment effects       ******
******          in bivariate probit               ******  
******       instrumentalvariable models          ******    
********************************************************
******                                            ******  
******                4/18/2017                   ******         
******     this file runs stata code to estimate  ******
******      the Stata  models for the application ******
********************************************************

**** this file requires the ivreg2 command in the ivreg2 package 
**** please install via command "ssc install ivreg2, replace"
**** ranktest version 01.3.02 or higher is also required


set more off, permanently
**** set appropriate working directory


use "perry.data.dta"

set seed 12345

biprobit (antvote = diplomaonly) (diplomaonly = treatment), vce(bootstrap, reps(500))
matrix result = J(8,3,.)

matrix result[1,1] = _b[diplomaonly:_cons]
matrix result[2,1] = _se[diplomaonly:_cons]
matrix result[3,1] = _b[diplomaonly:treatment]
matrix result[4,1] = _se[diplomaonly:treatment]


matrix result[5,1] = _b[antvote:_cons]
matrix result[6,1] = _se[antvote:_cons]
matrix result[7,1] = _b[antvote:diplomaonly]
matrix result[8,1] = _se[antvote:diplomaonly]


ivregress2 2sls antvote (diplomaonly = treatment), first
est restore first
est store stage1_1
est restore second
est store stage2_1
ivregress2 liml antvote (diplomaonly = treatment), first 
est restore first
est store stage1_2
est restore second
est store stage2_2


clear
use "ihad.data.dta"

set seed 12345

biprobit (antvote = diplomaonly) (diplomaonly = treatment), vce(bootstrap, reps(500))

matrix result[1,2] =  _b[diplomaonly:_cons]
matrix result[2,2] = _se[diplomaonly:_cons]
matrix result[3,2] = _b[diplomaonly:treatment]
matrix result[4,2] = _se[diplomaonly:treatment]


matrix result[5,2] =  _b[antvote:_cons]
matrix result[6,2] =  _se[antvote:_cons]
matrix result[7,2] =  _b[antvote:diplomaonly]
matrix result[8,2] =  _se[antvote:diplomaonly]



ivreg2 antvote (diplomaonly = treatment), fuller(1) first
est restore first
est store stage1_3
est restore second
est store stage2_3
ivregress2 liml antvote (diplomaonly = treatment), first 
est restore first
est store stage1_4
est restore second
est store stage2_4



clear
use "star.data.dta"

set seed 12345

biprobit (antvote = diplomaonly) (diplomaonly = treatment), vce(bootstrap, reps(500))

matrix result[1,3] = _b[diplomaonly:_cons]
matrix result[2,3] =  _se[diplomaonly:_cons]
matrix result[3,3] =  _b[diplomaonly:treatment]
matrix result[4,3] =  _se[diplomaonly:treatment]


matrix result[5,3] =  _b[antvote:_cons]
matrix result[6,3] =  _se[antvote:_cons]
matrix result[7,3] =  _b[antvote:diplomaonly]
matrix result[8,3] =  _se[antvote:diplomaonly]

ivregress2 2sls antvote (diplomaonly = treatment), first
est restore first
est store stage1_5
est restore second
est store stage2_5
ivregress2 liml antvote (diplomaonly = treatment), first 
est restore first
est store stage1_6
est restore second
est store stage2_6

**** output table in csv with biprobit results and latex tables with 2SLS results
putexcel set results_biprobit, replace
putexcel A1 = matrix(result)

esttab stage2* using "SondheimerGreen_2sls.tex", label se star(* 0.1 ** 0.05 *** 0.01) obslast replace 
esttab stage1* using "SondheimerGreen_2sls_first.tex", label se star(* 0.1 ** 0.05 *** 0.01) obslast replace 

log close
